package com.zhihuichongcheng.application.ui.task.adapter.holder;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.task.SubTaskBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.task.SubTaskDetailActivity;
import com.zhihuichongcheng.application.util.BroadCastUtils;

import java.util.HashMap;

/**
 * subTaskHolder
 *
 * Created by LK on 2017/1/17.
 */
public class SubTaskHolder extends BaseHolder {
    private TextView tvName, tvCreateUser, tvFinishTime, tvStatus;
    private LinearLayout llItem;

    public SubTaskHolder(View itemView, BaseActivity activity) {
        super(itemView, activity);
        tvName = (TextView) itemView.findViewById(R.id.sub_task_name);
        tvCreateUser = (TextView) itemView.findViewById(R.id.sub_task_creat_user);
        tvFinishTime = (TextView) itemView.findViewById(R.id.sub_task_finish_time);
        tvStatus = (TextView) itemView.findViewById(R.id.sub_task_status);
        llItem = (LinearLayout) itemView.findViewById(R.id.ll_sub_task);
    }

    public void setData(final SubTaskBean data) {
        tvName.setEnabled(data.isLive());
        tvCreateUser.setEnabled(data.isLive());
        tvFinishTime.setEnabled(data.isLive());
        tvStatus.setEnabled(data.isLive());

        tvName.setText(data.getTitle());
        tvCreateUser.setText(data.getUserName());
        tvFinishTime.setText(data.getFinishTime());
        tvStatus.setText(data.getStateMsg());

        llItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt(SubTaskDetailActivity.EXTRA_TASK_ID, data.getTaskId());
                bundle.putInt(SubTaskDetailActivity.EXTRA_SUB_TASK_ID, data.getSubTaskId());
                startActivity(SubTaskDetailActivity.class, bundle);
            }
        });

        llItem.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (data.getUserId() == Integer.parseInt(app.getUser().getUserId()))
                    new MaterialDialog.Builder(itemView.getContext())
                            .title("请选择")
                            .items("结束任务", "删除任务")
                            .itemsCallback(new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
                                    if (position == 0) {
                                        if (data.isLive())
                                            closeTask(data);
                                        else
                                            ToastOpt.createToast(getContext(), "任务已关闭");
                                    } else if (position == 1) {
                                        delTask(data);
                                    }
                                }
                            })
                            .show();
                return true;
            }
        });
    }

    /**
     * 关闭子任务
     */
    private void closeTask(SubTaskBean data) {
        DialogUtil.showLoadDiadlog(getContext());
        HashMap<String, String> paramsDemo = new HashMap<>();
        paramsDemo.put("token", mUser.getToken());
        paramsDemo.put("userMissionId", data.getTaskId() + "");
        paramsDemo.put("subtaskId", data.getSubTaskId() + "");
        manager.post(NetworkAPI.MISSION_SUBTASK_FINISH, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                DialogUtil.dismiss(getContext());
                if (code == 2) {
                    ToastOpt.createToast(getContext(), msg);
                    return;
                }
                BroadCastUtils.getInstance().actionRefreshSubTaskList(getContext());
            }

            @Override
            public void onFailure(String value) {
                ToastOpt.createToast(getContext(), "关闭失败 " + value);
                DialogUtil.dismiss(getContext());
            }
        }, paramsDemo);
    }

    /**
     * 删除子任务
     */
    private void delTask(SubTaskBean data) {
        DialogUtil.showLoadDiadlog(getContext());
        HashMap<String, String> paramsDemo = new HashMap<>();
        paramsDemo.put("token", mUser.getToken());
        paramsDemo.put("userMissionId", data.getTaskId() + "");
        paramsDemo.put("subtaskId", data.getSubTaskId() + "");
        manager.post(NetworkAPI.MISSION_SUBTASK_DELETE, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                DialogUtil.dismiss(getContext());
                if (code == 2) {
                    ToastOpt.createToast(getContext(), msg);
                    return;
                }
                BroadCastUtils.getInstance().actionRefreshSubTaskList(getContext());
            }

            @Override
            public void onFailure(String value) {
                ToastOpt.createToast(getContext(), "删除失败 " + value);
                DialogUtil.dismiss(getContext());
            }
        }, paramsDemo);
    }
}
